function [sig,Fsamp,Channels,Bits]=loadwav(filename,slen);

% Function LOADWAV: loading of WAV-formated signal to vector
%                   <-1,1> range - according to number of bits.
%
% Usage: [sig,fsamp,channels,bits]=loadwav(filename[,slen]);
%   filename   - name of WAV-file with signal
%
% Optional parameters:                                             <Defualt values>
%   slen       - desired length of loaded signal       <according length of signal>
%
% Output:
%   sig        - output vector/matrix (format as follows)
%             s1(1),   s2(1),  ...,  sn(1),
%             s1(2),   s2(2),  ...,  sn(2),
%             s1(3),   s2(3),  ...,  sn(3),
%               :        :      :      : 
%             1-st.,   2-nd.,   :    n-th channel
%   channels   - number of signal channels
%   bits       - number of bits per sample
%   fsamp      - sampling frequency
 
%                              Made by PP
%                            20 March 1998
%                           CTU FEL,  Prague


if nargin==1,
  slen=inf;
end;
if nargin>2,
  disp('loadwav: Too many parameters of loadwav !');
  return;
end;


F=fopen(filename,'r');
if F==-1,
  disp('Error: Enable to open signal !');
  return;
end;

% typedef unsigned int  word ;
% typedef unsigned long dword ;
%
% /*********************  Hlavicka WAV souboru   ***************************/

%   typedef struct {
%   char  acRIFF[4];       /* "RIFF"                                    */
%   dword dwRiffLen;       /* remaining bytes in File                   */
%   char  acWave[4];       /* "WAVE"                                    */
%   char  acFmt[4];        /* "fmt"                                     */
%   dword dwFmtLen;        /* 16, lenght of format block following      */
%   word  wFormatTag;      /* 1 (WAVE_FORMAT_PCM), others reserved      */
%   word  wChannels;       /* 1=mono, 2=stereo                          */
%   dword dwSamplesPerSecond; /* in Hz (11025, 22050,44100,..)          */
%   dword dwAvgBytesPerSec;/* (dwSamplesPerSecond * wBlockAlign)        */
%   word  wBlockAlign;     /* (wBitsPerSample * wChannels / 8)          */
%   word  wBitsPerSample;  /* 8 or 16                                   */
%   char  acData[4];       /* "DATA"                                    */
%   dword dwDataLen;       /* lenght of sample data (bytes!)            */
% } WAVEFILE ;

% /* In case of wChannels == 2, left and right channel data is found in alternating
% 	 sequence. */

acRIFF=lower(setstr(fread(F,4,'char')'));
dwRiffLen=fread(F,1,'int32');
acWave=lower(setstr(fread(F,4,'char')'));
if ~(acRIFF=='riff' & acWave=='wave')
  disp('loadwav: Signal is not RIFF WAVE format !');
  return ;
end 

acFmt=lower(setstr(fread(F,4,'char')'));
if ~(acFmt(1:3)=='fmt')
  disp('loadwav: Enable to find format blok !');
  return ;
end 

dwFmtLen=fread(F,1,'int32');

FormatTag=fread(F,1,'int16');
Channels=fread(F,1,'int16');
Fsamp=fread(F,1,'int32');
AvgBytesPerSec=fread(F,1,'int32');
BlockAlign=fread(F,1,'int16');
Bits=fread(F,1,'int16');


acData=lower(setstr(fread(F,4,'char')'));
if ~(acFmt(1:4)=='Data')
  disp('loadwav: Enable to find DATA blok !');
  return  ;
end 


dwDataLen=fread(F,1,'int32');
if slen > 8*dwDataLen/Bits,
  slen=8*dwDataLen/Bits;
end;

if Bits==8,
  Maxint=2^Bits;
  [sig,count]=fread(F,slen,'uint8'); 
  sig=sig./Maxint ; sig=sig-mean(sig) ;
elseif Bits==16
  Maxint=2^(Bits-1);
  [sig,count]=fread(F,slen,'int16');
  sig=sig./Maxint ; 
else
  disp('loadwav: Only 8 and 16 bit format is supported !');
  return ;
end ;

%plot(sig)

disp(sprintf('%.0f samples were read!',count));

fclose(F);

yy=reshape(sig,Channels,slen/Channels);
sig=yy';

%plot(sig)




